package cate.game.activity.moonfight;

import cate.common.table.activity.moonfight.row.MoonHardLevelRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.game.activity.moonfight.msg.MoonFightChallengeResultResp;
import cate.game.activity.moonfight.msg.MoonFightGlobalInfoResp;
import cate.game.play.part.PlaySide;
import cate.game.play.proce.play.PlayPO;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;

public class MoonFightFTC extends AbstractFarmFTC<MoonFightActivity> {
    public MoonHardLevelRow row;
    public PlaySide ps;
    public int rank;

    public MoonFightFTC(Role role, int funcId, MoonHardLevelRow row, PlaySide ps, int rank) {
        this.funcId = funcId;
        this.row = row;
        this.ps = ps;
        this.rank = rank;
        setup(role);
    }

    @Override
    protected MoonFightActivity po(Role role) {
        return role.getActivity().getEnableActivitySingle(GDFunc.MOON_FIGHT);
    }

    @Override
    public void handleResult(Role role, PlayPO playPO) {
        super.handleResult(role, playPO);
        boolean win = playPO.winner == GDFight.Team.A;
        if (win) {
            MoonFightActivity mf = po(role);
            //先更新个人战斗记录
            mf.updatePassRecord(row.level, this.rank, row.hardLevel);
            //再判断是否需要替换全服数据
            int h = role.getGame().table.moonFight.hardLevel.getHardest(row.level);
            role.getHistory().action.moonFightWinTimes(row.hardLevel, 1);
            if (row.hardLevel >= h) {
                MoonFightActivityHandler handler = role.getGame().activityGlobal.getEnableHandler(funcId);
                handler.getGlobalData().updateBoss(row.level, rank, role.getUid(), ps);
                role.getGame().role.activeForOnline(r -> {
                    r.sendNow(new MoonFightGlobalInfoResp(handler.getGlobalData()));
                });
            }
        }
        MoonFightChallengeResultResp resp = new MoonFightChallengeResultResp();
        resp.funcId = funcId;
        resp.round = row.level;
        resp.rank = rank;
        resp.hard = row.hardLevel;
        resp.win = win;
        role.sendNow(resp);
    }
}
